<?php
/**
  * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说 图/谱/源/码/分/享
 */
defined("IN_IA")or exit("Access denied");
global $_GPC,$_W;
$uniacid=$_GPC['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] :"index";
$uid=$_GPC['uid'];

//加入购物车
if($op=='addCart'){
	$goods_id=$_GPC['goods_id'];
	$spec_id=$_GPC['spec_id'];
	$count=$_GPC['count'];
	$goodsData=pdo_get('cqkundian_farm_goods',array('id'=>$goods_id,'uniacid'=>$uniacid));
	if($goodsData['is_open_sku']==1){	//有规格 
		$cartData=pdo_get('cqkundian_farm_cart',array('goods_id'=>$goods_id,'uniacid'=>$uniacid,'spec_id'=>$spec_id,'uid'=>$uid));
		//判断购物车中是否有该规格的商品
		if($cartData){
			$res=pdo_update('cqkundian_farm_cart',array('count +='=>1),array('goods_id'=>$goods_id,'uniacid'=>$uniacid,'spec_id'=>$spec_id,'uid'=>$uid));
		}else{
			
			$specVal=pdo_get('cqkundian_farm_goods_spec',array('id'=>$spec_id,'uniacid'=>$uniacid));
			$insertData=array(
				'uid'=>$uid,
				'goods_id'=>$goods_id,
				'spec_id'=>$spec_id,
				'uniacid'=>$uniacid,
				'count'=>$count,
				'goods_name'=>$goodsData['goods_name'],
				'price'=>$specVal['price'],
				'create_time'=>time(),
			);
			if($specVal['spec_src']==''){
				$insertData['cover']=$goodsData['cover'];
			}else{
				$insertData['cover']=$specVal['spec_src'];
			}
			$res=pdo_insert('cqkundian_farm_cart',$insertData);
		}
	}else{	//无规格
		$cartData=pdo_get('cqkundian_farm_cart',array('goods_id'=>$goods_id,'uniacid'=>$uniacid,'uid'=>$uid));
		if($cartData){
			$res=pdo_update('cqkundian_farm_cart',array('count +='=>1),array('goods_id'=>$goods_id,'uniacid'=>$uniacid,'uid'=>$uid));
		}else{
			
			$insertData=array(
				'uid'=>$uid,
				'goods_id'=>$goods_id,
				'uniacid'=>$uniacid,
				'count'=>$count,
				'goods_name'=>$goodsData['goods_name'],
				'price'=>$goodsData['price'],
				'create_time'=>time(),
			);
			$insertData['cover']=$goodsData['cover'];
			$res=pdo_insert('cqkundian_farm_cart',$insertData);
		}
	}
	echo $res ? json_encode(array('code'=>1)) : json_encode(array('code'=>2));die;
}

//购物车列表
if($op=='cartList'){
	$request=array();
	if(!empty($_GPC['page'])){
		$page=$_GPC['page']+1;
	}else{
		$page=1;
	}
	$cartData=pdo_getall('cqkundian_farm_cart',array('uid'=>$uid,'uniacid'=>$uniacid),'','','create_time desc',array($page,10));

	for($i=0;$i<count($cartData);$i++){
		$specVal=pdo_get('cqkundian_farm_goods_spec',array('goods_id'=>$cartData[$i]['goods_id'],'id'=>$cartData[$i]['spec_id'],'uniacid'=>$uniacid));
		$spec_id_arr=explode(',',$specVal['sku_name']);
		$specItem=pdo_getAll('cqkundian_farm_spec',array('goods_id'=>$cartData[$i]['goods_id'],'uniacid'=>$uniacid));
		$spec_val=pdo_getAll('cqkundian_farm_spec_value',array('id in'=>$spec_id_arr,'uniacid'=>$uniacid));
		for ($m=0; $m < count($specItem); $m++) {
			for($n=0;$n<count($spec_val);$n++){
				if($specItem[$m]['id']==$spec_val[$n]['spec_id']){
					$specItem[$m]['spec_val']=$spec_val[$n]['spec_value'];
				}
			}
		}
		$cartData[$i]['check']=false;
		$cartData[$i]['specItem']=$specItem;
	}
	$request['cartData']=$cartData;
	echo json_encode($request);die;
}


//减少购物车数量
if($op=='reducuCount'){
	$id=$_GPC['id'];
	$condition=array(
		'id'=>$id,
		'uniacid'=>$uniacid,
		'uid'=>$uid,
	);
	$cartData=pdo_get('cqkundian_farm_cart',$condition);
	if($cartData['count']==1){	//当购物车数量为1时，删除该条数据
		$res=pdo_delete('cqkundian_farm_cart',$condition);
	}else{
		$res=pdo_update('cqkundian_farm_cart',array('count -='=>1),$condition);
	}
	$cartData=pdo_get('cqkundian_farm_cart',$condition);
	echo $res ? json_encode(array('code'=>1,'count'=>$cartData['count'])) : json_encode(array('code'=>2));die;
}

//增加购物车数量
if($op=='addCount'){
	$id=$_GPC['id'];
	$condition=array(
		'id'=>$id,
		'uniacid'=>$uniacid,
		'uid'=>$uid,
	);
	$res=pdo_update('cqkundian_farm_cart',array('count +='=>1),$condition);
	$cartData=pdo_get('cqkundian_farm_cart',$condition);
	echo $res ? json_encode(array('code'=>1,'count'=>$cartData['count'])) : json_encode(array('code'=>2));die;
}

//购物车去结算
if($op=="getBuyCartData"){
	$request=array();
	$cart_id_str=$_GPC['cart_id'];
	$cart_id=explode("_",$cart_id_str);
	$uid=$_GPC['uid'];
	$condition=array();
	$condition=array(
		'id in'=>$cart_id,
		'uniacid'=>$uniacid,
		'uid'=>$uid,
	);
	$cartData=pdo_getall('cqkundian_farm_cart',$condition);
	$totalPrice=0;
    $goodsData=array();
	for($i=0;$i<count($cartData);$i++){
		$specVal=pdo_get('cqkundian_farm_goods_spec',array('goods_id'=>$cartData[$i]['goods_id'],'id'=>$cartData[$i]['spec_id'],'uniacid'=>$uniacid));
		$spec_id_arr=explode(',',$specVal['sku_name']);
		$specItem=pdo_getall('cqkundian_farm_spec',array('goods_id'=>$cartData[$i]['goods_id'],'uniacid'=>$uniacid));
		$spec_val=pdo_getall('cqkundian_farm_spec_value',array('id in'=>$spec_id_arr,'uniacid'=>$uniacid));
		for ($m=0; $m < count($specItem); $m++) {
			for($n=0;$n<count($spec_val);$n++){
				if($specItem[$m]['id']==$spec_val[$n]['spec_id']){
					$specItem[$m]['spec_val']=$spec_val[$n]['spec_value'];
				}
			}
		}
		$cartData[$i]['item_price']=number_format($cartData[$i]['price']*$cartData[$i]['count'],2);
		$cartData[$i]['check']=false;
		$cartData[$i]['specItem']=$specItem;
		$totalPrice+=$cartData[$i]['price']*$cartData[$i]['count'];
        $cartData[$i]['price']=number_format($cartData[$i]['price'],2);
        $goodsData[]=pdo_get('cqkundian_farm_goods',array('uniacid'=>$uniacid,'id'=>$cartData[$i]['goods_id']));
	}

	$request['cartData']=$cartData;
	$aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
	$request['aboutData']=$aboutData;
    $send_price=0;
    for($j=0;$j<count($goodsData);$j++){
        $send_price+=$goodsData[$j]['send_price'];
    }
	$totalPrice+=$send_price;
	$request['totalPrice']=number_format($totalPrice,2);
	$request['send_price']=number_format($send_price,2);

    //查看用户是否有优惠券
    $couponCount=0;
    $userCoupon=pdo_getall('cqkundian_farm_user_coupon',array('uniacid'=>$uniacid,'uid'=>$uid,'status'=>0));
    if(!empty($userCoupon)) {
        for ($i = 0; $i < count($userCoupon); $i++) {
            $coupon = pdo_get('cqkundian_farm_shop_coupon', array('uniacid' => $uniacid, 'id' => $userCoupon[$i]['cid'], 'type' => 1));
            //优惠券是否过期
            if ($coupon['expiry_date'] == 1) {
                if (($userCoupon[$i]['create_time'] + ($coupon['expiry_day'] * 86400)) > time()) {
                    if ($coupon['low_cash_price'] < $totalPrice) {  //判断订单金额是否大于优惠消费最低金额
                        $couponCount += 1;
                    }
                }
            } else {
                if (time() > $coupon['begin_time'] && time() < $coupon['end_time']) {
                    if ($coupon['low_cash_price'] < $totalPrice) {  //判断订单金额是否大于优惠消费最低金额
                        $couponCount += 1;
                    }
                }
            }
        }
    }
    $request['couponCount']=$couponCount;
	echo json_encode($request);die;
}

//删除购物车信息
if($op=='deleteCart'){
	$id=$_GPC['id'];
	$res=pdo_delete('cqkundian_farm_cart',array('id'=>$id,'uniacid'=>$uniacid,'uid'=>$uid));
	echo $res ? json_encode(array('code'=>1)) : json_encode(array('code'=>2));die;
}



